# -*- coding : utf-8 -*-
# coding:utf-8

"""
REST统一响应和错误定义
"""

from flask_restful import abort


class ResponseCode:
    SUCCESS = 0
    ERROR = 1
    WRONG_PARAM = 30
    PKL_NOT_FOUND = 113

    MESSAGE_TEXT = {
        SUCCESS: '成功！',
        ERROR: '未知错误！',
        WRONG_PARAM: '参数错误！',
        PKL_NOT_FOUND: '请求的deviceCode没有找到对应的pkl文件！'
    }


def generate_response(datas=None, message=None, code=ResponseCode.SUCCESS, appendmessage=None):
    if message == None:
        message = ResponseCode.MESSAGE_TEXT[code]
    if appendmessage != None:
        message = message + appendmessage
    return {
        'resp_code': code,
        'resp_msg': message,
        'datas': datas
    }


def my_abort(http_status_code, *args, **kwargs):
    if http_status_code == 400:
        # 重定义400返回参数
        abort(400, **generate_response(datas=[kwargs.get('message')], message=ResponseCode.MESSAGE_TEXT[ResponseCode.WRONG_PARAM], code=ResponseCode.WRONG_PARAM))

    abort(http_status_code)
